<template>
  <view v-if="shenheStatus == 0" class="chooseType">
    <view class="typeList">
      <view class="typeItem" @click="toWrite('subject_info')">
        <view class="status">
          <text :class="subject_info_status ? 'red' : ''"></text>
        </view>
        <view class="info">
          <view>
            <text>{{ $t('salesman.main') }}</text>
            <text>{{ $t('salesman.mainInfo') }}</text>
          </view>
          <text class="iconfont icon-arrow-right"></text>
        </view>
      </view>
      <view class="typeItem" @click="toWrite('business_info')">
        <view class="status">
          <text :class="business_info_status ? 'red' : ''"></text>
        </view>
        <view class="info">
          <view>
            <text>{{ $t('salesman.business') }}</text>
            <text>{{ $t('salesman.busInfo') }}</text>
          </view>
          <text class="iconfont icon-arrow-right"></text>
        </view>
      </view>
      <view class="typeItem" @click="toWrite('bank_account_info')">
        <view class="status">
          <text :class="bank_account_info_status ? 'red' : ''"></text>
        </view>
        <view class="info">
          <view>
            <text>{{ $t('salesman.account') }}</text>
            <text>{{ $t('salesman.accInfo') }}</text>
          </view>
          <text class="iconfont icon-arrow-right"></text>
        </view>
      </view>
      <view class="typeItem" @click="toWrite('contact_info')">
        <view class="status">
          <text :class="contact_info_status ? 'red' : ''"></text>
        </view>
        <view class="info">
          <view>
            <text>{{ $t('salesman.admin') }}</text>
            <text>{{ $t('salesman.adminInfo') }}</text>
          </view>
          <text class="iconfont icon-arrow-right"></text>
        </view>
      </view>
    </view>
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from '@/components/shenhe/shenhe';
export default {
  components: { Shenhe },
  data() {
    return {
      // 申请的数据
      application: {
        id: 0,
        contact_info: {
          contact_name: '',
          contact_id_number: '',
          mobile_phone: '',
          contact_email: '',
        },
        subject_info: {
          // 主体类型 SUBJECT_TYPE_INDIVIDUAL 个体户 SUBJECT_TYPE_ENTERPRISE 企业 SUBJECT_TYPE_INSTITUTIONS 党政机关单位 SUBJECT_TYPE_OTHERS 其他组织
          subject_type: '',
          business_license_info: {
            license_copy: '',
            license_number: '',
            merchant_name: '',
            legal_person: '',
          },
          certificate_info: {
            is_long: 0,
            cert_copy: '',
            cert_type: '',
            cert_number: '',
            merchant_name: '',
            company_address: '',
            legal_person: '',
            period_begin: '',
            period_end: '',
          },
          organization_info: {
            is_composite: 0,
            organization_copy: '',
            organization_code: '',
            org_period_begin: '',
            org_period_end: '',
            is_long: 0,
          },
          identity_info: {
            is_long: 0,
            id_doc_type: '',
            id_card_info: {
              id_card_copy: '',
              id_card_national: '',
              id_card_name: '',
              id_card_number: '',
              card_period_begin: '',
              card_period_end: '',
            },
            id_doc_info: {
              id_doc_copy: '',
              id_doc_name: '',
              id_doc_number: '',
              doc_period_begin: '',
              doc_period_end: '',
            },
          },
        },
        business_info: { merchant_shortname: '', service_phone: '' },
        bank_account_info: {
          bank_account_type: '',
          account_name: '',
          account_bank: '',
          bank_address_code: '',
          account_number: '',
          code_list: [],
          address_list: [],
        },
      },
    };
  },
  onLoad(option) {
    if (!option.id || option.id != 0) {
      this.getApply(option.id, option.type);
    } else {
      if (option.type) {
        this.application.subject_info.subject_type = option.type;
      }
    }
  },
  onShow() {
    if (uni.getStorageSync('application')) {
      this.application = uni.getStorageSync('application');
    }
  },
  methods: {
    // 跳转填写资料页面
    toWrite(e) {
      uni.navigateTo({
        url:
          '/salesman/application/write?type=' +
          e +
          '&application=' +
          JSON.stringify(this.application),
      });
    },
    // 根据id获取申请表单详情
    async getApply(id, type) {
      const res = await this.$allrequest.merchant.detail({ id }, true);
      if (res.code == 0) {
        uni.removeStorageSync('application');
        if (type) {
          this.application.subject_info.subject_type = type;
        }
        if (res.data.subject_info) {
          this.application.subject_info = res.data.subject_info;
        }
        if (res.data.bank_account_info) {
          this.application.bank_account_info = res.data.bank_account_info;
        }
        if (res.data.business_info) {
          this.application.business_info = res.data.business_info;
        }
        if (res.data.contact_info) {
          this.application.contact_info = res.data.contact_info;
        }
        this.application.id = res.data.id;
      }
    },
    // 表单验证
    checkForm(type) {
      // 处于主体信息填写页面
      if (type == 'subject_info' || type == 'all') {
        if (
          this.application.subject_info.subject_type ==
            'SUBJECT_TYPE_INDIVIDUAL' ||
          this.application.subject_info.subject_type ==
            'SUBJECT_TYPE_ENTERPRISE'
        ) {
          if (
            this.application.subject_info.business_license_info.license_copy ==
            ''
          )
            return false;
          if (
            this.application.subject_info.business_license_info
              .license_number == ''
          )
            return false;
          if (
            this.application.subject_info.business_license_info.merchant_name ==
            ''
          )
            return false;
          if (
            this.application.subject_info.business_license_info.legal_person ==
            ''
          )
            return false;
        }
        if (
          this.application.subject_info.subject_type ==
            'SUBJECT_TYPE_ENTERPRISE' ||
          this.application.subject_info.subject_type ==
            'SUBJECT_TYPE_INSTITUTIONS' ||
          this.application.subject_info.subject_type == 'SUBJECT_TYPE_OTHERS'
        ) {
          if (
            this.application.subject_info.subject_type ==
              'SUBJECT_TYPE_INSTITUTIONS' ||
            this.application.subject_info.subject_type == 'SUBJECT_TYPE_OTHERS'
          ) {
            if (this.application.subject_info.certificate_info.cert_copy == '')
              return false;
            if (this.application.subject_info.certificate_info.cert_type == '')
              return false;
            if (
              this.application.subject_info.certificate_info.cert_number == ''
            )
              return false;
            if (
              this.application.subject_info.certificate_info.merchant_name == ''
            )
              return false;
            if (
              this.application.subject_info.certificate_info.company_address ==
              ''
            )
              return false;
            if (
              this.application.subject_info.certificate_info.legal_person == ''
            )
              return false;
            if (
              this.application.subject_info.certificate_info.period_begin == ''
            )
              return false;
            if (this.application.subject_info.certificate_info.is_long == 0) {
              if (
                this.application.subject_info.certificate_info.period_end == ''
              )
                return false;
            }
          }
          if (
            this.application.subject_info.organization_info.is_composite == 0
          ) {
            if (
              this.application.subject_info.organization_info
                .organization_copy == ''
            )
              return false;
            if (
              this.application.subject_info.organization_info
                .organization_code == ''
            )
              return false;
            if (
              this.application.subject_info.organization_info
                .org_period_begin == ''
            )
              return false;
            if (this.application.subject_info.organization_info.is_long == 0) {
              if (
                this.application.subject_info.organization_info
                  .org_period_end == ''
              )
                return false;
            }
          }
        }
        if (
          this.application.subject_info.identity_info.id_doc_type ==
          'IDENTIFICATION_TYPE_IDCARD'
        ) {
          if (
            this.application.subject_info.identity_info.id_card_info
              .id_card_copy == ''
          )
            return false;
          if (
            this.application.subject_info.identity_info.id_card_info
              .id_card_national == ''
          )
            return false;
          if (
            this.application.subject_info.identity_info.id_card_info
              .id_card_name == ''
          )
            return false;
          if (
            this.application.subject_info.identity_info.id_card_info
              .id_card_number == ''
          )
            return false;
          if (
            this.application.subject_info.identity_info.id_card_info
              .card_period_begin == ''
          )
            return false;
          if (this.application.subject_info.identity_info.is_long == 0) {
            if (
              this.application.subject_info.identity_info.id_card_info
                .card_period_end == ''
            )
              return false;
          }
        } else {
          if (
            this.application.subject_info.identity_info.id_doc_info
              .id_doc_copy == ''
          )
            return false;
          if (
            this.application.subject_info.identity_info.id_doc_info
              .id_doc_name == ''
          )
            return false;
          if (
            this.application.subject_info.identity_info.id_doc_info
              .id_doc_number == ''
          )
            return false;
          if (
            this.application.subject_info.identity_info.id_doc_info
              .doc_period_begin == ''
          )
            return false;
          if (this.application.subject_info.identity_info.is_long == 0) {
            if (
              this.application.subject_info.identity_info.id_doc_info
                .doc_period_end == ''
            )
              return false;
          }
        }
      }
      // 处于经营信息填写页面
      if (type == 'business_info' || type == 'all') {
        if (this.application.business_info.merchant_shortname == '')
          return false;
        if (this.application.business_info.service_phone == '') return false;
      }
      // 处于结算账户填写页面
      if (type == 'bank_account_info' || type == 'all') {
        if (this.application.bank_account_info.bank_account_type == '')
          return false;
        if (this.application.bank_account_info.account_name == '') return false;
        if (this.application.bank_account_info.account_bank == '') return false;
        if (this.application.bank_account_info.bank_address_code == '')
          return false;
        if (this.application.bank_account_info.account_number == '')
          return false;
      }
      // 处于超级管理员页面
      if (type == 'contact_info' || type == 'all') {
        if (this.application.contact_info.contact_name == '') return false;
        if (this.application.contact_info.contact_id_number == '') return false;
        if (this.application.contact_info.mobile_phone == '') return false;
        if (this.application.contact_info.contact_email == '') return false;
      }
    },
  },
  computed: {
    subject_info_status() {
      let flag = this.checkForm('subject_info') == false ? false : true;
      return flag;
    },
    business_info_status() {
      let flag = this.checkForm('business_info') == false ? false : true;
      return flag;
    },
    bank_account_info_status() {
      let flag = this.checkForm('bank_account_info') == false ? false : true;
      return flag;
    },
    contact_info_status() {
      let flag = this.checkForm('contact_info') == false ? false : true;
      return flag;
    },
  },
  onUnload() {
    wx.removeStorageSync('application');
  },
};
</script>

<style lang="scss" scoped>
.typeList {
  padding-left: 30rpx;
  font-family: PingFang SC;
  color: #262626;
  .typeItem {
    padding: 40rpx 30rpx 40rpx 0;
    border-bottom: 1px solid #f8f8f8;
    display: flex;
    .status {
      width: 10rpx;
      min-width: 10rpx;
      height: 10rpx;
      padding-top: 16rpx;
      text {
        display: block;
        width: 10rpx;
        height: 10rpx;
        border-radius: 50%;
        background-color: #e5e5e5;
      }
      .red {
        background-color: #f0250e;
      }
    }
    .info {
      flex: auto;
      display: flex;
      align-items: center;
      margin-left: 20rpx;
      justify-content: space-between;
      view {
        font-size: 28rpx;
        color: #8c8c8c;
        display: flex;
        margin-right: 40rpx;
        flex-direction: column;
        text:nth-child(1) {
          font-size: 32rpx;
          font-weight: 600;
          color: #262626;
          margin-bottom: 10rpx;
        }
      }
      .iconfont {
        font-size: 24rpx;
        color: #888;
      }
    }
  }
}
</style>
